//
//  WSSendMealTimeFooterView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/2.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSSendMealTimeFooterView.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h" //富文本

@interface WSSendMealTimeFooterView()
/**1.背景View*/
@property (nonatomic, strong) UIView *bgView;
/**2.送餐时间Lb*/
@property (nonatomic, strong) UILabel *titleLb;
/**第0块*/
@property (nonatomic, strong) UIView *div0;

/**第一块*/
@property (nonatomic, strong) UIView *div1;
/**尽快送达按钮*/
@property (nonatomic, strong) UIButton *btn1;
/**尽快送达lb*/
@property (nonatomic, strong) UILabel *lb1;


/**第2块*/
@property (nonatomic, strong) UIView *div2;
/**预约送达l按钮*/
@property (nonatomic, strong) UIButton *btn2;
/**预约送达lb*/
@property (nonatomic, strong) YYLabel *lb2;


@end

@implementation WSSendMealTimeFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.div0];
    [self.div0 addSubview:self.titleLb];
    
//    [self.bgView addSubview:self.titleLb]; //送餐时间
    [self.bgView addSubview:self.div1];
    [self.bgView addSubview:self.div2];

    [self.div1 addSubview:self.btn1];
    [self.div1 addSubview:self.lb1];
    
    [self.div2 addSubview:self.btn2];
    [self.div2 addSubview:self.lb2];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.div0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.bgView);
        make.height.mas_equalTo(40);
    }];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div0).offset(15);
        make.centerY.equalTo(self.div0.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];

    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left);
        make.top.equalTo(self.div0.mas_bottom).offset(1);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(40);
    }];
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left);
        make.right.equalTo(self.div1.mas_right);
        make.top.equalTo(self.div1.mas_bottom).offset(1);
        make.height.mas_equalTo(40);
    }];
    
    [self layoutDiv1];
    [self layoutDiv2];
}

- (void)layoutDiv1 {
    [self.btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(self.div1.mas_centerY);
    }];
    [self.lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btn1.mas_right).offset(5);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(251, 22));
    }];
}
- (void)layoutDiv2 {
    [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(self.div2.mas_centerY);
    }];
    [self.lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btn2.mas_right).offset(5);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(251, 22));
    }];
}


#pragma mark - getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = kMainLightGrayColor;
    }
    return _bgView;
}


//第0块
- (UIView *)div0 {
    if (!_div0) {
        _div0 = [UIView new];
        _div0.backgroundColor = [UIColor whiteColor];
    }
    return _div0;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.text = @"送餐时间";
    }
    return _titleLb;
}




//第一块=====================
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
        _div1.backgroundColor = [UIColor whiteColor];
    }
    return _div1;
}
- (UIButton *)btn1 {
    if (!_btn1) {
        _btn1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btn1 setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_btn1 setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
        [_btn1 addTarget:self action:@selector(clickBtn1:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _btn1;
}
- (UILabel *)lb1 {
    if (!_lb1) {
        _lb1 = [UILabel new];
        _lb1.text = @"尽快送达 (预计40分钟送达)";
    }
    return _lb1;
}



//第二块=====================
- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
        _div2.backgroundColor = [UIColor whiteColor];
    }
    return _div2;
}
- (YYLabel *)lb2 {
    if (!_lb2) {
        _lb2 = [YYLabel new];
        _lb2.text = @"预约送达";
    }
    return _lb2;
}

- (UIButton *)btn2 {
    if (!_btn2) {
        _btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btn2 setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_btn2 setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
        [_btn2 addTarget:self action:@selector(clickBtn2:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _btn2;
}

#pragma mark - 点击事件
- (void)clickBtn1:(UIButton *)btn
{
    RBFunctionLog;
    
    btn.selected = YES;
    self.btn2.selected = NO;
}

- (void)clickBtn2:(UIButton *)btn{
    btn.selected = YES;
    self.btn1.selected = NO;
    RBFunctionLog;
    
    if (self.clickYuYueBlock) {
        //把当前 ”预约按钮“  传递出去，需要修改显示的文字
        //内部修改
        self.clickYuYueBlock();
    }
}

#pragma mark - 修改预约 时间
- (void)updateYuYueTimeStr:(NSString *)timeStr {
    UIColor *blackColor = [UIColor blackColor];
    UIColor *redColor = [UIColor redColor];
    UIColor *clearColor = [UIColor clearColor];
    
    NSString *time = [NSString stringWithFormat:@"今日%@",timeStr];
    
    NSString *str = [NSString stringWithFormat:@"预约 %@ 送达",time];
    NSRange r1 = [str rangeOfString:time];
    
    
    NSDictionary *attDic1 =   @{NSForegroundColorAttributeName:blackColor,NSFontAttributeName:[UIFont systemFontOfSize:16.0]};
    
    NSMutableAttributedString *attStr1 = [[NSMutableAttributedString alloc] initWithString:str attributes:attDic1];
    [attStr1 yy_setTextHighlightRange:r1 color:redColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
    }];
    self.lb2.attributedText = attStr1;
}
@end
